﻿using Developer.Code;
using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Controllers
{
    public class ReviewController : BaseController
    {
        // GET: Review
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            int pageCount;
            int itemCount;

            var reviews = ReviewService.GetAll().Where(a => a.IsAbort == false).ToList();
            var list = reviews.OrderByDescending(a => a.CreateTime)
                .Page(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: PageSize);
            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(list);
        }

        public ActionResult GetReview(int rid)
        {
            var review = ReviewService.GetAll().Find(rid);
            return View(review);
        }

        public ActionResult MyReviews(int uid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            int pageCount;
            int itemCount;
            PageSize = 5;
            var reviews = ReviewService.GetAll().Where(a => a.UserId == uid && a.IsAbort == false).ToList();
            var list = reviews.OrderByDescending(a => a.CreateTime)
                .Page(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: PageSize);
            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            ViewBag.uid = uid;
            return View(list);
        }

        [HttpPost]
        public JsonResult SetSubscribe()
        {
            var subscribe = Query.Value("subscribe").ToInt();
            var rid = Query.Value("rid").ToInt();
            var review = ReviewService.GetAll().Find(rid);
            review.Subscribe = subscribe;
            ReviewService.Change(review);
            return Json(new { success = 1 }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult EditReview(int? rid)
        {
            var uid = Query.Value("uid").ToInt();
            Reviews review = new Reviews();
            if (rid.HasValue)
            {
                review = ReviewService.GetAll().Find(rid);
            }
            ViewBag.uid = uid;
            return View(review);
        }
        
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditReview(FormCollection form)
        {
            var uid = Query.Value("uid").ToInt();
            var rid = Query.Value("rid").ToInt();
            if (rid > 0)
            {
                var review = ReviewService.GetAll().Find(rid);
                review.Contents = form["Contents"];
                review.CreateTime = DateTime.Now;
                review.Img = Query.Value("Img");
                review.SubTitle = Query.Value("SubTitle");
                review.Title = Query.Value("Title");
                ReviewService.Change(review);
                return Redirect("/Review/MyReviews?uid=" + uid);
            }
            else
            {
                var review = new Reviews
                {
                    Contents = form["Contents"],
                    CreateTime = DateTime.Now,
                    Img = Query.Value("Img"),
                    IsAbort = false,
                    Subscribe = 0,
                    SubTitle = Query.Value("SubTitle"),
                    Title = Query.Value("Title"),
                    UserId = uid
                };
                ReviewService.Add(review);
                return Redirect("/Review/MyReviews?uid=" + uid);
            }
        }
           [HttpPost]
        public ActionResult UpLoadImages(HttpPostedFileBase file)
        {
            if (file != null)
            {
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var files = Request.Files[i];
                    var fileName = Path.GetFileName(file.FileName);
                    string fileType = Path.GetExtension(file.FileName);
                    //获取上传文件的后缀   
                    if (fileName != "")
                    {
                        string newName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +
                            DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +
                            DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + fileType;
                        //更改上传文件名   
                        string Currentpath = Server.MapPath("/attached/UpLoadImages");
                        CreateDirectory ImgDr = new CreateDirectory();
                        ImgDr.CreatePath(Currentpath);
                        var path = Currentpath + "\\" + newName;
                        files.SaveAs(path);

                        string RelativePath = "/attached/UpLoadImages" + "/" + newName;

                        //GetUrl url = new GetUrl();

                        //string retPath = url.GetUrlStr().ToString() + RelativePath;

                        return Content(RelativePath);
                    }
                }
            }
            return View();
        }
    
    }
}